//
//  NetworkingTool.m
//  BaseAPP
//
//  Created by Evisu on 16/11/21.
//  Copyright © 2016年 evisum. All rights reserved.
//

#import "NetworkingTool.h"

@implementation NetworkingTool

+ (void)GETByURL:(NSString *)urlString parameters:(id)parameters success:(successBlock)successBlock failure:(errorBlock)errorBlock {
    
    //网络监测
    [NetworkingTool ReachabilityStatus:^(id netStatus) {
        
        NSLog(@"%@", netStatus);
    }];
    
    //初始化
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html", nil];
    manager.requestSerializer.timeoutInterval = 60.f;//请求超时
    
    [manager GET:urlString parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
        //下载进度
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //请求成功
        if (successBlock) {
            successBlock(responseObject);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        //请求失败
        if (errorBlock) {
            errorBlock(error);
        }
    }];
    
}

+ (void)POSTByURL:(NSString *)urlString parameters:(id)parameters success:(successBlock)successBlock failure:(errorBlock)errorBlock {
    
    //网络监测
    [NetworkingTool ReachabilityStatus:^(id netStatus) {
        
        NSLog(@"%@", netStatus);
    }];
    
    //初始化
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html", nil];
    manager.requestSerializer.timeoutInterval = 60.f;//请求超时
    
    [manager POST:urlString parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
        //下载进度
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        //请求成功
        if (successBlock) {
            successBlock(responseObject);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        //请求失败
        if (errorBlock) {
            errorBlock(error);
        }
    }];
    
}

+ (void)uploadPostByURL:(NSString *)urlString parameters:(id)parameters image:(NSMutableArray *)imageArray success:(successBlock)successBlock failure:(errorBlock)errorBlock {

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/html", nil];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer.timeoutInterval = 60.f;
    
    [manager POST:urlString parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        for (UIImage *image in imageArray) {
            NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
            formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
            NSString *fileName = [NSString stringWithFormat:@"%@.png",[formatter stringFromDate:[NSDate date]]];
            NSData *data = UIImageJPEGRepresentation(image, 0.7);
            [formData appendPartWithFileData:data name:@"uploadimage" fileName:fileName mimeType:@"image/png"];
        }
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        //上传进度
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //上传成功
        if (successBlock) {
            successBlock(responseObject);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        //上传失败
        if (errorBlock) {
            errorBlock(error);
        }
        
    }];
}

+ (void)ReachabilityStatus:(netStatusBlock)netStatus {

    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        
        switch (status) {
            case AFNetworkReachabilityStatusUnknown:
                if (netStatus) {
                    netStatus(@"未知网络");
                }
                break;
                
            case AFNetworkReachabilityStatusNotReachable:
                if (netStatus) {
                    netStatus(@"无可用网络");
                }
                break;
                
            case AFNetworkReachabilityStatusReachableViaWWAN:
                if (netStatus) {
                    netStatus(@"蜂窝数据网络");
                }
                break;
                
            case AFNetworkReachabilityStatusReachableViaWiFi:
                if (netStatus) {
                    netStatus(@"wifi数据网络");
                }
                break;
                
            default:
                break;
        }
        
    }];
    
    [manager startMonitoring];
}

@end
